﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Clueless
{
    public partial class ChooseCharacter : Form
    {
        /// <summary>
        /// Event called when the accept button is pressed.
        /// Sends the selected text via event args
        /// </summary>
        public event EventHandler<CharacterSelectedEventArgs> CharacterSelected;

        /// <summary>
        /// Generic
        /// </summary>
        public ChooseCharacter()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Adds all of the names from the list to the lstBox
        /// </summary>
        /// <param name="names"></param>
        public ChooseCharacter(LinkedList<String> names)
        {
            InitializeComponent();

            foreach (String s in names)
            {

                lstChars.Items.Add(s);
            }
        }


        public delegate void LinkedStringDelegate(LinkedList<String> s);
        /// <summary>
        /// Set the list of characters to the names
        /// </summary>
        /// <param name="names"></param>
        public void SetCharacterList(LinkedList<String> names)
        {
            if (lstChars.InvokeRequired)
                lstChars.Invoke(new LinkedStringDelegate(SetCharacterList), names);
            else
            {
                foreach (String s in names)
                {
                    lstChars.Items.Add(s);
                }
            }
        }

        /// <summary>
        /// Accept the choice of characters and report to the main thread
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (lstChars.SelectedItems.Count == 1)
            {
                String character = lstChars.Items[lstChars.SelectedIndex].ToString();  // Might not be right

                SendCharacterSelectedMessage(character);

                this.Close();
            }
        }

        /// <summary>
        /// Loads the name into the event args and sends it back to the main form
        /// </summary>
        /// <param name="character"></param>
        private void SendCharacterSelectedMessage(String character)
        {
            CharacterSelectedEventArgs args = new CharacterSelectedEventArgs();
            args.Name = character;
            OnCharacterSelectedMessage(args);
        }


        /// <summary>
        /// Call the event trigger
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnCharacterSelectedMessage(CharacterSelectedEventArgs e)
        {
            EventHandler<CharacterSelectedEventArgs> handler = CharacterSelected;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        
    }

    /// <summary>
    /// Event args for a character selection action
    /// </summary>
    public class CharacterSelectedEventArgs : EventArgs
    {
        public String Name { get; set; }
    }
}
